﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Service
{

    public class RequestManage
    {
        private EKAASBIMEntities dbContext;
        public RequestManage()
        {
            dbContext = new EKAASBIMEntities();
        }

        public List<requirementRecords> getRecords(string user)
        {
            return dbContext.requirementRecords.Where(o => o.employee == user).ToList();
        }

        public List<requirementRecords> getRecordsBystatus(int status)
        {
            //           return dbContext.requirementRecords.Where(o=>o.status==Sta).ToList();
            return dbContext.requirementRecords.Where(o => o.status == status).ToList();
        }

        public List<requirementRecords> Receive(string[] reqmags, requirementRecords com)
        {
            List<requirementRecords> records = new List<requirementRecords>();
            foreach (string reqmag in reqmags)
            {
                int id = default(int);
                int.TryParse(reqmag, out id);
                var d = dbContext.requirementRecords.SingleOrDefault(o => o.ID == id);
                if (d != null)
                {
                    d.status = 1;
                    d.comments = com.comments;
                    records.Add(d);
                }
            }

            dbContext.SaveChanges();
            return records;
        }

        public List<requirementRecords> Pass(string[] reqmags, requirementRecords com)
        {
            List<requirementRecords> records = new List<requirementRecords>();
            foreach (string reqmag in reqmags)
            {
                int id = default(int);
                int.TryParse(reqmag, out id);
                var d = dbContext.requirementRecords.SingleOrDefault(o => o.ID == id);
                if (d != null)
                {
                    d.status = 2;
                    d.comments = com.comments;
                    records.Add(d);
                }
            }

            dbContext.SaveChanges();
            return records;
        }

        public List<requirementRecords> Reject(string[] reqmags, requirementRecords com)
        {
            List<requirementRecords> records = new List<requirementRecords>();
            foreach (string reqmag in reqmags)
            {
                int id = default(int);
                int.TryParse(reqmag, out id);
                var d = dbContext.requirementRecords.SingleOrDefault(o => o.ID == id);
                if (d != null)
                {
                    d.status = 3;
                    d.comments = com.comments;
                    records.Add(d);
                }
            }

            dbContext.SaveChanges();
            return records;
        }
    }
}
